<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body style="height: 3000px;">
  <div id="app">
    <button @click="isShow = !isShow">切换组件状态</button>
    <common-head v-if="isShow"></common-head>
  </div>
  <script src="./vue.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({
      data() {
        return {
          isShow: true
        }
      }
    });
    const CommonHead = {
      template: `
        <div>
          <h2>这是组件1</h2>
        </div>
      `,
      data() {
        return {
          timer: null
        }
      },
      mounted() {
        console.log('dom渲染完成 mounted');
        this.timer = setInterval(() => {
          console.log(1);
        }, 1000)

        window.onscroll = function(){
          console.log('页面滚动');
        }
      },
      beforeUnmount() {
        console.log('beforeUnmount 卸载之前');
        clearInterval(this.timer);
        window.onscroll = null;
      }
    }
    app.component('CommonHead', CommonHead)


    app.mount('#app')
  </script>
</body>

</html>